<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Auth {
	private static $_instance = null;
	private $_id = 0;

	public function __clone() {
        throw new Node_Exception('Can`t clone auth object');
    }

    private static function &instance() {
        if (!isset(self::$_instance)) {
            $c = __CLASS__;
            self::$_instance = new $c;
        }

        return self::$_instance;
    }
	
    public static function factory() {
        $auth = self::instance();
		$id = Session::get('auth_id', $auth->_id);

		if (!empty($id)) {
			self::login($id);
		}

		return $id;
    }

	public static function isLogged() {
		$result = false;
		
		if (self::getId() > 0) {
			$result = true;
		}
		
		return $result;
    }

	public static function login($id) {
        $auth = self::instance();
		$auth->_id = intval($id);
		Session::set('auth_id', $auth->_id);
	}

	public static function logout() {
        $auth = self::instance();
		$auth->_id = 0;
		Session::set('auth_id', $auth->_id);
	}

	public static function getId() {
		return self::instance()->_id;
	}
}
